package day_2022_12_to_3.day;

/**
 * @author haomin
 * @date 2022/12/24 21:59
 **/
public class Day12_24_Num1754 {
    class Solution {
        public String largestMerge(String word1, String word2) {
            StringBuffer sb = new StringBuffer();
            while(word1.length() != 0 && word2.length() != 0) {
                if(word1.charAt(0) > word2.charAt(0)) {
                    sb.append(word1.charAt(0));
                    word1 = word1.substring(1, word1.length());
                }else if(word1.charAt(0) < word2.charAt(0)){
                    sb.append(word2.charAt(0));
                    word2 = word2.substring(1, word2.length());
                }else {
                    int i = 0;
                    while(i < word1.length() && i < word2.length() && word1.charAt(i) == word2.charAt(i)) i++;
                    if(i < word1.length() && i < word2.length()) {
                        if(word1.charAt(i) > word2.charAt(i)) {
                            sb.append(word1.charAt(0));
                            word1 = word1.substring(1, word1.length());
                        }else {
                            sb.append(word2.charAt(0));
                            word2 = word2.substring(1, word2.length());
                        }
                    }else {
                        if(i == word2.length()) {
                            sb.append(word1.charAt(0));
                            word1 = word1.substring(1, word1.length());
                        }else {
                            sb.append(word2.charAt(0));
                            word2 = word2.substring(1, word2.length());
                        }
                    }
                }
            }
            if(word1 != null) sb.append(word1);
            if(word2 != null) sb.append(word2);
            return sb.toString();
        }
    }
}